package tt.dz.order.schedulingjob;

import com.alibaba.druid.util.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.order.pojo.OrderAlreadyPaid;
import tt.dz.order.util.ExcelUtill;
import tt.dz.order.util.MailUtils;
import tt.dz.order.vo.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhoutian
 * @Title 定时统计运营商当日金额
 * @date 2015-11-17
 */
@Component
public class SendMailJob extends QuartzJobBean {
    private static Logger log = LoggerFactory.getLogger(SendMailJob.class);


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("定时邮件 start");
        try {
            Calendar nowCalendar = Calendar.getInstance();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            nowCalendar.add(Calendar.DAY_OF_MONTH, -1);
            String startDatestr = dateFormat.format(nowCalendar.getTime());
            nowCalendar.setTime(dateFormat.parse(startDatestr));
            Date startDate = nowCalendar.getTime();
            List<OrderAlreadyPaid> list = OrderAlreadyPaidVo.queryByDate(startDate);
            if (list != null) {
                StringBuilder sb;
                MerchantVO merchantVO = new MerchantVO();
                File file;
                for (OrderAlreadyPaid ord : list) {
                    sb = new StringBuilder();
                    sb.append(ord.getMerchantCodeName()).append(startDatestr).append("日交易流水:").append(ord.getPrice()).append("元");
                    QueryOrderResportVO queryOrderResportVO = new QueryOrderResportVO();
                    queryOrderResportVO.setMerchantCode(ord.getMerchantCode());
                    queryOrderResportVO.setStartDate(startDate);
                    List<ExportOrderItem> orderlist = OrderQueryVO.export(queryOrderResportVO);
                    LinkedHashMap<String, String> fieldMap = new LinkedHashMap();
                    fieldMap.put("订单号", "orderno");
                    fieldMap.put("所属电站", "servicestationno");
                    fieldMap.put("电桩编号", "equipmentno");
                    fieldMap.put("用户卡号", "cardno");
                    fieldMap.put("开启方式", "cardtype");
                    fieldMap.put("服务类型", "name");
                    fieldMap.put("电量", "buynum");
                    fieldMap.put("金额", "totalprice");
                    fieldMap.put("开始时间", "starttime");
                    fieldMap.put("结束时间", "endtime");
                    file = ExcelUtill.exportExcel("订单报表", fieldMap, orderlist, UUID.randomUUID().toString() + ".csv");
                    merchantVO = merchantVO.queryMerchantByCode(ord.getMerchantCode());
                    if (merchantVO != null && !StringUtils.isEmpty(merchantVO.getEmail())) {
                        MailUtils.sendMail(merchantVO.getEmail(), sb.toString(), file.getPath(), startDatestr+".csv");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("定时邮件 end");
    }

}
